package com.oracle.lingshangejia.activity.special;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.lws.basic.utils.common.AdapterIntegerDefault0;
import com.lws.basic.utils.common.DoubleDefault0Adapter;
import com.oracle.lingshangejia.Constant;
import com.oracle.lingshangejia.R;
import com.oracle.lingshangejia.adapter.special.SpecialDiseaseDetailAdapter;
import com.oracle.lingshangejia.bean.Cj1Bean;
import com.oracle.lingshangejia.bean.Cj25Bean;
import com.oracle.lingshangejia.bean.User;
import com.oracle.lingshangejia.model.OkgoNetwork;
import com.oracle.lingshangejia.mvp.base.BaseActivity;
import com.oracle.lingshangejia.utils.MyLogger;
import com.oracle.lingshangejia.utils.MyToast;
import com.oracle.lingshangejia.utils.ObserverOnNext;
import com.oracle.lingshangejia.utils.RxUtils;
import com.tencent.bugly.crashreport.CrashReport;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
//产检25次详情

public class Chanjian25DetailActivity extends BaseActivity {

    @Override
    public int setLayoutId() {
        return R.layout.activity_chanjian25_detail;
    }

    @BindView(R.id.rv)
    RecyclerView mRv;
    private SpecialDiseaseDetailAdapter mAdapter;

    @Override
    public void initData() {
        super.initData();
        ButterKnife.bind(this);
        initRecycelrView();
        getData();
    }

    private void initRecycelrView() {
        mRv.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new SpecialDiseaseDetailAdapter(null);
        mRv.setAdapter(mAdapter);
    }


    private void getData() {
        showLoading();
        Observable.create(new ObservableOnSubscribe<Cj25Bean>() {
            @Override
            public void subscribe(ObservableEmitter<Cj25Bean> emitter) throws Exception {
                int id = getIntent().getIntExtra("id", 0);
                Map<Object, Object> map = new HashMap<>();
                map.put("token", User.getToken());
                map.put("mark", 1);
                map.put("zxbId", id);
                JSONObject jsonObject = new JSONObject(map);
                MyLogger.i(jsonObject.toString());
                Gson gson = new GsonBuilder()
                        .registerTypeAdapter(Integer.class, new AdapterIntegerDefault0())
                        .registerTypeAdapter(int.class, new AdapterIntegerDefault0())
                        .registerTypeAdapter(Double.class, new DoubleDefault0Adapter())
                        .registerTypeAdapter(double.class, new DoubleDefault0Adapter())
                        .create();
                String json = OkgoNetwork.post(Constant.cj25_detail, jsonObject.toString());
                MyLogger.i(json);
                emitter.onNext(gson.fromJson(json,Cj25Bean.class));
            }
        }).compose(RxUtils.io_main()).subscribe(new ObserverOnNext<Cj25Bean>() {
            @Override
            public void onNext(Cj25Bean bean) {
                try {
                    dismissLoading();
                    if (bean.getData() != null) {
                        List<String> list = new ArrayList<>();
                        list.add("孕周:  " + bean.getData().getYz());
                        list.add("主诉:  " + bean.getData().getZs());
                        list.add("体重(kg):  " + bean.getData().getTz());
                        list.add("宫底高度(cm):  " + bean.getData().getCkjcGdgd());
                        list.add("腹围(cm):  " + bean.getData().getCkjcFw());
                        list.add("胎位:  " + bean.getData().getTz());
                        list.add("胎心率(次/分钟):  " + bean.getData().getCkjcTxl());
                        list.add("血压(mmHg) 收缩率:  " + bean.getData().getXySsl() + "      舒张率:  " + bean.getData().getXySzl());
                        list.add("血红蛋白(g/L):  " + bean.getData().getXgdb());
                        list.add("尿蛋白:  " + bean.getData().getNdb());
                        list.add("其他辅助检查:  " + bean.getData().getQtfzjc());
                        list.add("体重(kg):  " + bean.getData().getTz());
                        list.add("分类:  " + (bean.getData().getFl() == 2 ? bean.getData().getFlYc() : "未见异常"));
                        list.add("指导:  \n" +
                                zd(bean.getData().getZdList(), bean.getData().getZdQt()));
                        if (bean.getData().getZzjy() == 2) {// 1无 2有
                            list.add("转诊原因：" + bean.getData().getZzyy());
                            list.add("机构科室：" + bean.getData().getZzjg()
                                    + "    " + bean.getData().getZzks());
                        }

                        list.add("时间：" + bean.getData().getCreateDate());
                        mAdapter.setNewData(list);
                    } else {
                        MyToast.show(bean.getMsg());
                    }
                }catch (Exception e){
                    MyToast.show(e.getMessage());
                    CrashReport.postCatchedException(e);
                }
            }
        });

    }

    //指导
    private String zd(List<String> list ,String qt){
        String s ="";
        for (int i =0; i<list.size() ;i++) {
            switch (list.get(i)) {
                case "1":
                    s += "生活方式  ";
                    break;
                case "2":
                    s += "营养  ";
                    break;
                case "3":
                    s += "心理  ";
                    break;
                case "4":
                    s += "运动  ";
                    break;
                case "5":
                    s += "自我监护   ";
                    break;
                case "6":
                    s += "分娩准备  ";
                    break;
                case "7":
                    s += "母乳喂养   ";
                    break;
                case "8":
                    s +=qt;
                    break;


            }
        }
        return s;

    }


}
